<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <link rel="stylesheet" href="../../../static/layuiadmin/layui/css/layui.css"
          th:href="@{/layuiadmin/layui/css/layui.css}"
          media="all">
    <link rel="stylesheet" href="../../../static/layuiadmin/style/admin.css"
          th:href="@{/layuiadmin/style/admin.css}"
          media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">代码生成</div>
                <div class="layui-card-body">
                    <div class="layui-btn-group test-table-operate-btn" style="margin-bottom: 10px;">
                        <button class="layui-btn" data-type="genCheckCode">生成选中代码</button>
                    </div>
                    <form id="articleSearch" class="layui-form layui-form-pane" method="get" action=""
                          style="margin-top: 20px;">
                        <div class="test-table-reload-btn" style="margin-bottom: 10px;">
                            表名称：
                            <div class="layui-inline">
                                <input class="layui-input" name="tableName" id="tableName" autocomplete="off">
                            </div>&nbsp;&nbsp;&nbsp;&nbsp;
                            <button class="layui-btn" type="button"
                                    data-type="reload">搜索
                            </button>
                        </div>
                    </form>

                    <table class="layui-hide" id="tables" lay-filter="tables"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="row_opt">
    <a class="layui-btn layui-btn-xs" lay-event="genCode" shiro:hasPermission="tool:gen:code">生成代码</a>
</script>
<script src="../../../static/jquery/jquery.min.js" th:src="@{/jquery/jquery.min.js}"></script>
<script src="../../../static/layuiadmin/layui/layui.js" th:src="@{/layuiadmin/layui/layui.js}"></script>
<script th:inline="javascript"> var ctx = [[@{
    /}]]; var captchaType = [[${captchaType}]];
</script>
<script th:inline="none">
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['form', 'index', 'table', 'laydate'], function () {
        /*var kw = "";
        addData(kw);*/

        var table = layui.table;
        table.render({
            elem: '#tables'
            , id: 'tbReload'
            , method: 'GET'
            , url: ctx + 'back/tool/gen/list'
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , title: '用户数据表'
            , fitColumns: true
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'tableName', title: '表名称', sort: true}
                , {field: 'tableComment', title: '表描述'}
                , {field: 'createTime', title: '创建时间', sort: true}
                , {field: 'updateTime', title: '更新时间'}
                , {fixed: 'right', title: '操作', toolbar: '#row_opt'}

            ]]
            , page: true
        });
        var form = layui.form, layer = layui.layer;

        //监听行工具事件
        table.on('tool(tables)', function (obj) {
            var data = obj.data;
            if (obj.event === 'genCode') {
                window.location.href = ctx + 'back/tool/gen/genCode/' + data.tableName;
            }
        });

        var $ = layui.$, active = {
            genCheckCode: function () { //获取选中数据
                var checkStatus = table.checkStatus('tbReload')
                    , data = checkStatus.data;
                if (data.length == 0) {
                    layer.msg(`请选择操作数据`);
                    return;
                }
                var tableNames = "";
                for (var i in data) {
                    tableNames += data[i].tableName + ",";
                }
                location.href = ctx + "back/tool/gen/batchGenCode?tables=" + tableNames.substring(0, tableNames.lastIndexOf(','));
            },
            reload: function () {
                var tableName = $('#tableName').val();
                //执行重载
                table.reload('tbReload', {
                    where: {
                        tableName: tableName
                    }
                });
            }
        };

        $('.test-table-reload-btn .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        $('.test-table-operate-btn .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>